import _ from 'underscore'
import dispatcher from 'lib/dispatcher'
import {Scope} from 'lib/scopeFactory'

import TimeStore from 'lib/stores/time-store'
import {coreApi,sortMap,propConfig,parseToTopbar} from '../apis/core-util'

var classifyTree = [{
  key: 'ip'
}, {
  key: 'port',
  requireId: 'ip'
}];
export default class MainScope extends Scope {
  constructor(){
    super();
    this.viewType = '2';
    this.$state = {
      topbar:{
        $id:'topbar1',
        loading:true
      },
      sortOptions:[{
        key:'2',
        label:'Read耗时'
      },{
        key:'3',
        label:'Write耗时'
      }]
    };
    this.topbarApi = coreApi({
      viewKey:'socket_time',
      order:'2',
      limit:20,
      urlName:'socket',
      classify:'ip',
      size:1
    },TimeStore.getValue,() => {
      return {
        type:this.viewType
      }
    });
  }
  fetch(){
    this.topbarApi.fetch().then((data,params) => {
      this.$state.topbar = {
        data:parseToTopbar(data,params,{
          classifyTree:classifyTree
        })
      }
    }, (err) => {
      this.$state.topbar = {
        error:err
      }
    }, (params) => {
      this.$state.topbar = {
        loading:true,
        params:params
      }
      this.emit()
    }).always(() => {
      this.emit()
    })
  }
  $settime(payload){
    this.fetch()
  }
  $changesort(payload){
    this.viewType = payload.key;
    this.fetch()
  }
  $load(){
    this.fetch()
  }
}
